<div class="layuimini-container">
    <form id="app-form" class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label">套餐名称</label>
            <div class="layui-input-block">
                <input type="text" name="title" class="layui-input" lay-verify="required" placeholder="请输入套餐名称" value="{$row.title}">
            </div>
        </div>
        <div class="layui-form-item">
                    <label class="layui-form-label ">缩略图</label>
                    <div class="layui-input-block layuimini-upload">
                        <input name="thumb" class="layui-input layui-col-xs6"  placeholder="请上传缩略图图" value="{$row.thumb}">
                        <div class="layuimini-upload-btn">
                            <span><a class="layui-btn layui-btn-sm" data-upload="thumb" data-upload-number="one" data-upload-exts="png|jpg|ico|jpeg" data-upload-icon="image"><i class="fa fa-upload"></i> 上传</a></span>
                          <!--   <span><a class="layui-btn layui-btn-normal layui-btn-sm" id="select_cover_picture" data-upload-select="thumb" data-upload-number="one" data-upload-mimetype="image/*"><i class="fa fa-list"></i> 选择</a></span> -->
                        </div>
                    </div>
            </div>
             <div class="layui-form-item">
                    <label class="layui-form-label ">主图</label>
                    <div class="layui-input-block layuimini-upload">
                        <input name="main_image" class="layui-input layui-col-xs6"  placeholder="请上传主图" value="{$row.main_image}">
                        <div class="layuimini-upload-btn">
                            <span><a class="layui-btn layui-btn-sm" data-upload="main_image" data-upload-number="more" data-upload-exts="png|jpg|ico|jpeg" data-upload-icon="image"><i class="fa fa-upload"></i> 上传</a></span>
                          <!--   <span><a class="layui-btn layui-btn-normal layui-btn-sm" id="select_cover_picture" data-upload-select="thumb" data-upload-number="one" data-upload-mimetype="image/*"><i class="fa fa-list"></i> 选择</a></span> -->
                        </div>
                    </div>
            </div>
             <div class="layui-form-item">
                    <label class="layui-form-label ">详情图</label>
                    <div class="layui-input-block layuimini-upload">
                        <input name="detail_image" class="layui-input layui-col-xs6"  placeholder="请上传门票图" value="{$row.detail_image}">
                        <div class="layuimini-upload-btn">
                            <span><a class="layui-btn layui-btn-sm" data-upload="detail_image" data-upload-number="more" data-upload-exts="png|jpg|ico|jpeg" data-upload-icon="image"><i class="fa fa-upload"></i> 上传</a></span>
                          <!--   <span><a class="layui-btn layui-btn-normal layui-btn-sm" id="select_cover_picture" data-upload-select="thumb" data-upload-number="one" data-upload-mimetype="image/*"><i class="fa fa-list"></i> 选择</a></span> -->
                        </div>
                    </div>
            </div>
          <div class="layui-form-item">
                <label class="layui-form-label">套餐描述</label>
                <div class="layui-input-block">
                    <textarea id="description" rows="6" class="layui-textarea " placeholder="请输入套餐描述" name="description">{$row.description}</textarea>
                </div>
          </div>
           <div class="layui-form-item">
                <label class="layui-form-label">租赁设施</label>
                  <div class="layui-input-block">
                    <input type="text" id="dataTypes" lay-filter="buildingFilter" class="layui-input" readonly="readonly" name="lease_id">
                 </div>
            </div>
            <div class="layui-form-item layui-hide" id="lease">
            <label class="layui-form-label">设施对应数量：</label>
            <div class="layui-input-block">
                <table class="layui-table" id="sku-table">
                    <thead></thead>
                    <tbody></tbody>
                </table>
            </div>
        </div>
            <div class="layui-form-item">
                <label class="layui-form-label">商品</label>
                 <div class="layui-input-block">
                    <input type="text" id="dataTypesGoods" lay-filter="buildingFilter" class="layui-input" readonly="readonly" name="products_id">
                 </div>
            </div>
               <div class="layui-form-item layui-hide" id="goods">
            <label class="layui-form-label">商品对应数量：</label>
            <div class="layui-input-block">
                <table class="layui-table" id="sku-table_goods">
                    <thead></thead>
                    <tbody></tbody>
                </table>
            </div>
        </div>
          <div class="layui-form-item one " id="layui-one">
                    
            <div class="layui-form-item">
                <label class="layui-form-label">原价</label>
                <div class="layui-input-inline">
                    <input type="number" name="price" class="layui-input" lay-verify="required" placeholder="请输入原价" value="0">
                </div>
                
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">活动价</label>
                <div class="layui-input-inline">
                    <input type="number" name="activity_price" class="layui-input" lay-verify="required" placeholder="请输入活动价" value="0">
                </div>
            </div>
             <div class="layui-form-item">
                    <label class="layui-form-label">是否开启</label>
                    <div class="layui-input-block">
                      <input type="radio" name="status" value="1" title="是" checked>
                      <input type="radio" name="status" value="0" title="否">
                    </div>
                </div>
        </div>
   
        <div class="hr-line"></div>
        <div class="layui-form-item text-center">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm submit" data-url="edit">提交</button>
          
        </div>
        <input type="hidden" name="id" value="{$row.id}">
    </form>
    <input type="hidden" name="" value="{$row.goods}" id="checked_goods">
    <input type="hidden" name="" value="{$row.lease}" id="checked_lease">
</div>
<script src="__STATIC__/plugs/lay-v2.5.6/lay/modules/xm-select.js"></script>
<link rel="stylesheet" type="text/css" href="__STATIC__/plugs/lay_cascader/cascader/cascader.css"/>
<script src="__STATIC__/plugs/lay_cascader/cascader/cascader.js"></script>
<script>
     var layer = layui.layer,
        form = layui.form,
        $=layui.jquery,
        upload = layui.upload;
        var lease_id="{$row.lease_id}";
        var products_id="{$row.products_id}";
        var goods=$("#checked_goods").val();
        var lease=$("#checked_lease").val();
        if(goods){
            goods=JSON.parse(goods);
        }
        if(lease){
            lease=JSON.parse(lease);
        }
        // if(products_id){
            createSkuTableGoods(products_id)
        // }
         // if(lease_id){
            createSkuTable(lease_id)
        // }
    //多选下拉框渲染对象
 $.ajax({
        url:"{:url('meal.package/getLease')}",
        dataType: 'json',
        contentType: "application/json",  
        type: 'get',
        success: function (data) {
  
            layui.use('layCascader', function () {
                var layCascader = layui.layCascader;
                var demo1_1=   layCascader({
                        elem: '#dataTypes',
                        clearable:true,
                        props:{
                            multiple: true,
                            label: 'title',
                            value: 'id',
                        },
                        options: data.data,
                        value:lease_id?lease_id:""
                    });
                   demo1_1.changeEvent(function(){
                        var lease_id=$("#dataTypes").val();
                        createSkuTable(lease_id)
                    });
                
            });
        }
    });
$.ajax({
        url:"{:url('meal.package/getGoods')}",
        dataType: 'json',
        contentType: "application/json",  
        type: 'get',
        success: function (data) {
  
            layui.use('layCascader', function () {
                var layCascader = layui.layCascader;
                var demo1_1=   layCascader({
                        elem: '#dataTypesGoods',
                        clearable:true,
                        props:{
                            multiple: true,
                            label: 'products_title',
                            value: 'products_id',
                        },
                        options: data.data,
                         value:products_id?products_id:''
                    });
                   demo1_1.changeEvent(function(){
                        var products_id=$("#dataTypesGoods").val();
                         createSkuTableGoods(products_id)
                    });
                
            });
        }
    });
function createSkuTableGoods(products_id){
    if(products_id){
        $("#goods").removeClass('layui-hide');
    }else{
        $("#goods").addClass('layui-hide');
    }
    $('#sku-table_goods thead').html('<tr>' +
                '<th>商品名称</th>' +
                '<th>数量</th>' +
                '</tr>');
  $.get("{:url('meal.package/getGoods')}",{products_id:products_id},function(data){
            
                var prependTbodyTrs = [];
                data.data.forEach(function(item, index){
                      prependTbodyTrs.push('<tr>' +
                    '<td>'+item.products_title+'</td>' +
                    '<td><input type="text" name="goods['+item.products_id+']" value="'+(goods?goods[item.products_id]:1)+'" class="layui-input"  lay-verify="required|number" ></td>' +
                    '</tr>');
                })
            
        $('#sku-table_goods tbody').html(prependTbodyTrs.join(''));
        form.render('select');
    });  

}
function createSkuTable(lease_id){
    if(lease_id){
        $("#lease").removeClass('layui-hide');
    }else{
        $("#lease").addClass('layui-hide');
    }
    $('#sku-table thead').html('<tr>' +
                '<th>租赁名称</th>' +
                '<th>数量</th>' +
                '</tr>');
  $.get("{:url('meal.package/getLease')}",{lease_id:lease_id},function(data){
            
                var prependTbodyTrs = [];
                data.data.forEach(function(item, index){
                      prependTbodyTrs.push('<tr>' +
                    '<td>'+item.title+'</td>' +
                    '<td><input type="text" name="lease['+item.id+']" value="'+(lease?lease[item.id]:1)+'" class="layui-input"  lay-verify="required|number" ></td>' +
                    '</tr>');
                })
            
        $('#sku-table tbody').html(prependTbodyTrs.join(''));
        form.render('select');
    });  

}
</script>